package com.ficus.road.maintain.third.zh.bridgehotel;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.zh.bridgehotel.BridgeHotel;
import com.ficus.road.maintain.core.model.zh.bridgehotel.BridgeHotelWarning;
import com.ficus.road.maintain.core.model.zh.bridgehotel.BridgeHotelWarningZH;
import com.ficus.road.maintain.dao.BridgeHotelDao;
import com.ficus.road.maintain.dao.BridgeHotelWarningDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.ficus.road.maintain.util.MyDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/3/22 下午5:14
 */
@Component
@Slf4j
public class ZHBridgeHotelWarning extends BaseZH {

    @Autowired
    private BridgeHotelWarningDao bridgeHotelWarningDao;

    @Autowired
    private BridgeHotelDao bridgeHotelDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjGetBridgeHotelByDate";
        errMsg = "调用中海接口获取桥下空间告警数据异常!";
        successMsg = "调用中海接口获取桥下空间告警数据成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 12,14,16 * * ?", zone = "GMT+8:00")
    public void refreshBridgeHotelWarning() {
        Date date = new Date();
        for (int i = 0; i > -20; i--) {
            date = DateUtils.addDays(date, i);
            refreshDataByDate(date);
        }
    }

    public void refreshDataByDate(Date date) {
        String dateStr = MyDateUtil.dateFormatStr(date, "yyyy-MM-dd");
        arg1 = "{'date':'" + dateStr + "'}";
        String warningStr = null;
        int i = 0;
        while (i < 10) {
            warningStr = getZHData();
            if (ObjectUtils.isNotEmpty(warningStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(warningStr)) {
            log.warn("{}: 未同步到中海桥下空间告警信息！", dateStr);
            return;
        }
        List<BridgeHotelWarningZH> warningZHList = JSONArray.parseArray(warningStr, BridgeHotelWarningZH.class);
        for (BridgeHotelWarningZH zh : warningZHList) {
            if ("1".equals(zh.getIsalarm())) {
                log.info(warningStr);
                BridgeHotelWarning bridgeHotelWarning = BridgeHotelWarning.builder()
                        .deviceId(zh.getDeviceid())
                        .warningPic(zh.getAlarmpics())
                        .desc(zh.getDescription())
                        .warningTime(MyDateUtil.strParseDate(zh.getAlarmtime(), "yyyy-MM-dd'T'HH:mm:ss"))
                        .createTime(new Date())
                        .build();
                bridgeHotelWarningDao.saveOrUpdate(bridgeHotelWarning);
            }
            if (ObjectUtils.isNotEmpty(zh.getAlarmpics())) {
                BridgeHotel bridgeHotel = bridgeHotelDao.fetch(zh.getDeviceid());
                if (ObjectUtils.isEmpty(bridgeHotel)) {
                    return;
                }
                bridgeHotel.setPictureAddress(zh.getAlarmpics());
                bridgeHotel.setModificationTime(new Date());
                bridgeHotelDao.saveOrUpdate(bridgeHotel);
            }
        }
    }
}
